// by Tyrghen

private ["_object","_type","_type_name","_ammo_box"];

_object = _this select 0;

_type = typeof _object;

If (!X_Client) exitWith {};

if (!isnull _object) then {
	_ammo_vec = _object getVariable "d_ammotype";
	if (isnil "_ammo_vec") then { _ammo_vec = false; };
	// 1. Check that the vehicle is of the allowed types
	if (count d_check_ammo_load_vecs > 0) then {
		if (!(_type in d_check_ammo_load_vecs) && !_ammo_vec) exitWith { 
			[_object,"This vehicle cannot load an ammobox!"] call XfVehicleChat;
			diag_log Format["Cannot load ammobox in %1 (%2)",_type, _ammo_vec];
			_object =objNull; 
		};
	};
};
if (!isnull _object) then {
	// 2. Check that the vehicle is not destroyed
	if (!alive _object) exitWith {};
	// 3. Check that the vehicle has an ammob box already or not.
	_ammo_box = _object getVariable "d_ammobox";
	if (isNil "_ammo_box") then {_object setVariable ["d_ammobox",False,True];};
	if (_ammo_box) exitWith {[_object,"You already have an Ammo Box!!!"] call XfVehicleChat;};
	// 4. Only execute for the driver. No driver.. no ammobox.
	if (!(player == driver _object)) exitWith {diag_log "You're not the driver, no need to execute this"};
	// 5. We wait the either the vehicle stops or that he's out of the trigger list, or it's destroyed.
	waituntil { ((speed _object < 0.1) || !(_object in (list AmmoLoadTrigger)) || !alive _object) };
	// 6. We send a message to the driver
	_type_name = [_type,0] call XfGetDisplayName;
	diag_log Format["LoadVehicleBox %1 (%2,%3)", _type_name, X_Client,isDedicated];
	[_object,format ["Loading AmmoBox in %1... Please stand by...", _type_name]] call XfVehicleChat;
	sleep 5;
	// 7. If the vehicle is dead, we stop.
	if (!alive _object) exitWith {};
	// 8. If the vehicle is not in the trigger anymore, we stop.
	if (!(_object in (list AmmoLoadTrigger))) exitWith {[_object,"Box Loading Aborted"] call XfVehicleChat;};
	// 9. If all check passed, we set the variable.
	_object setVariable ["d_ammobox",True,True];
	[_object,"Ammo Box Loaded!"] call XfVehicleChat;
};